/*
 * TreeView.java
 *
 * Created on September 11, 2007, 10:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package common;

import java.io.File;
import javax.swing.*;
import javax.swing.tree.*;
/**
 *
 * @author user
 */
public class TreeView {
    
    /** Creates a new instance of TreeView */
    public TreeView() {
       
    }
   
    public static JScrollPane buildTree(JFileChooser chooser)
    {
        File file = chooser.getSelectedFile( );
        
        // create a hierarchy of nodes
        MutableTreeNode root = new DefaultMutableTreeNode("Projects");
        makeTree(file, root, 0);
        
        // create the JTree
        final DefaultTreeModel model = new DefaultTreeModel(root);
        final JTree tree = new JTree(model);
        
        JScrollPane scroll = new JScrollPane(tree);
        return scroll; 
    }
    
    private static void makeTree(File file, MutableTreeNode root, int index)
    {
        MutableTreeNode child = new DefaultMutableTreeNode(file.getName());
        root.insert(child, index);
        
        if( file.isDirectory())
        {
            File [] files = file.listFiles();
            for( int i=0; i < files.length; i++)
            {
                makeTree(files[i], child, i);
            }
        }
    }
}
